<?php

namespace app\manage\controller;

use core\libs\db;
use core\libs\input;

class works extends base {
    // 初始化
    public function __init() {
        $this->input = input::request(
            'id', 'action', 
            'search_key', 'works_number'
        );

        // 是否有权限
        $this->is_level($this->baseClass . "_list");
        if(in_array($this->baseAction, ["delete", "batch"])) {
            $this->is_level($this->baseClass . "_edit");
        }
    }
    // 编辑
    public function edit() {
        extract($this->input);

        $this->assign("row", db::fetch("pa_0804_works", "works_id = '$id'"));
        $this->assign("global", [
            "id" => $id
        ]);
        $this->display("works_add");
    }
    // 添加、更新
    public function submit() {
        extract($this->input);

        $form_datas = array(
            'works_number' => (int)$works_number
        );
        foreach($form_datas as $key => $val) {
            $form_datas[$key] = html_entity_decode($val);
        }

        if(empty($id)){
			//添加内容
			db::insert("pa_0804_works", $form_datas);	
			alert('内容添加成功!', U($this->baseClass, $this->getArgs()));		
		}
		else{
			//更新	
			db::update("pa_0804_works", $form_datas, "works_id = '$id'");
			alert('内容更新成功!', U($this->baseClass, $this->getArgs()));
		}
    }
    // 列表
    public function index() {
        extract($this->input);

        $filter = '';	
        if($search_key) {
            $filter.=" and works_id = '$search_key'";
        }

        $sql = "user_id = works_uid $filter";
        $page = new \core\libs\page((int)db::getCount("pa_0804_works, pa_0804_user", $sql), 10);
        $page->args = $this->getArgs("", 1);
        $result = db::fetchAll(
            "pa_0804_works, pa_0804_user", 
            "$sql order by works_id desc" . $page->limit
        );

        $this->assign("search", [
            'key' => $search_key
        ]);
        $this->assign("global", [
            "page" => $page,
        ]);
        $this->assign("result", $result);
        $this->display("works_list");
    }
    // 删除
    public function delete() {
        extract($this->input);

        if(!$id) alert('请先选择内容后在进行操作!', U($this->baseClass, $this->getArgs()));
        if(is_array($id)) $id = implode(",", $id);

        //删除内容
		db::delete('pa_0804_works', "works_id in ($id)");

        alert('删除成功!', U($this->baseClass, $this->getArgs()));		
    }
    // 批量操作
    public function batch() {
        extract($this->input);
        if(!$action) alert('请选择要操作的事件!', U($this->baseClass, $this->getArgs()));
        if(!$id) alert('请先选择内容后在进行操作!', U($this->baseClass, $this->getArgs()));
        if(is_array($id)) $id = implode(",", $id);

        // 审核
        if($action == "state_yes" || $action == "state_no") {
            //更新	
            $state = array("state_yes" => 1, "state_no" =>0);
            db::update(
                "pa_0804_works", array('works_state' => $state[$action]), "works_id in ($id)"
            );
            alert('更新成功!', U($this->baseClass, $this->getArgs()));		
        }

        // 删除
        if($action == "delete") {
            $this->delete();
        }
    }
    // 导出
    public function export() {
        $result = db::fetchAll("apply");

        $lists = [];
        foreach($result as $val) {
            if($val['apply_type']==1) {
                $apply_subject = $val['apply_subject'];
            }
            else {
                $apply_subject = [];
                foreach(json_decode($val['apply_subject'], true) as $v) {
                    if(is_array($v["val"])) {
                        foreach($v["val"] as $v2) {
                            $apply_subject[] = $v2;
                        }
                    }
                    else {
                        $apply_subject[] = $v["name"] . "::" . $v["val"];
                    }
                }
                $apply_subject = implode(",", $apply_subject);
            }

            $lists[] = [
                $val["apply_id"],
                $val["apply_team"],
                $val['apply_type']==1 ? "产品破解赛" : "漏洞复现赛",
                $apply_subject,
                $val['apply_teammate'],
                date('Y-m-d H:i:s', $val['apply_time'])
            ];
        }

        $datas = array(
			"title" => array('编号', '团队名称', '类型', '参加项目', '团队成员', '报名时间'),
			"lists" => $lists
        );
        $excel = new \core\libs\excel($datas, 'apply');
        $excel->export(); 
    }
    // 获取固定地址栏参数
    protected function getArgs($args = "", $type = 0) {
        extract($this->input);
        $baseurl = "?search_key={$search_key}{$args}";
        return $this->get_base_args($baseurl, $type);
    }
}